package com.todod.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.todod.entity.ResultData;
import com.todod.mapper.UsersManualCatalogMapper;
import com.todod.model.UsersManualCatalog;
import com.todod.model.UsersManualFile;
import com.todod.service.IUsersManualCatalogService;
import com.todod.service.IUsersManualFileService;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * @author lql
 * @date 2020年12月25日
 */
@RestController
@RequestMapping("/usersManualCatalog")
public class UsersManualCatalogController extends BaseManageController {

	@Autowired
	public IUsersManualCatalogService usersManualCatalogService;
	@Autowired
	public UsersManualCatalogMapper usersManualCatalogMapper;
	@Autowired
	private IUsersManualFileService usersManualFileService;

	/**
	 * 
	 * @param _userId
	 * @param request
	 * @param queryUsersManualFile
	 * @return
	 */
	@RequestMapping("/getTreeList")
	public ResultData getTreeList(@RequestAttribute Long _userId, Long xiangmuId) {

		// 获取个人创建数据
		List<UsersManualCatalog> list = usersManualCatalogService.getListByXmId(xiangmuId);

		JSONObject jo = new JSONObject();
		JSONArray ja = JSONArray.fromObject(jo);
		ja.remove(0);

		jo.put("id", xiangmuId);
		jo.put("name", "用户手册目录");
		jo.put("parentId", "-1");
		jo.put("xmId", xiangmuId);
		jo.put("sortNum", "0");
		jo.put("flag", "ML");
		ja.add(jo);

		if (list != null && list.size() > 0) {
			for (UsersManualCatalog usersManualCatalog1 : list) {
				jo.put("id", usersManualCatalog1.getId());
				jo.put("name", usersManualCatalog1.getName());
				jo.put("parentId", usersManualCatalog1.getParentId());
				jo.put("xmId", usersManualCatalog1.getXmId());
				jo.put("sortNum", usersManualCatalog1.getSortNum());
				jo.put("flag", "ML");
				ja.add(jo);

				// 获取个人创建数据
				List<UsersManualFile> plist = usersManualFileService.getListByClassifyId(usersManualCatalog1.getId());
				for (UsersManualFile usersManualFile : plist) {
					jo.put("id", usersManualFile.getId());
					jo.put("name", usersManualFile.getTableName());
					jo.put("parentId", usersManualFile.getClassifyId());
					jo.put("xmId", usersManualFile.getXmId());
					jo.put("sortNum", usersManualFile.getSortNum());
					jo.put("flag", "WD");
					jo.put("code", usersManualFile.getCode());
					ja.add(jo);
				}
			}
		}

		return ResultData.success("ok", ja);
	}

	/**
	 * 
	 * @param _userId
	 * @param reqUsersManualCatalog
	 * @return
	 */
	@PostMapping("/save")
	public ResultData saveData(@RequestAttribute Long _userId, @RequestBody UsersManualCatalog usersManualCatalog) {

		usersManualCatalogService.saveData(_userId, usersManualCatalog);
		return ResultData.success();
	}

	/**
	 * 
	 * @param _userId
	 * @param reqUsersManualCatalog
	 * @return
	 */
	@PostMapping("/edit")
	public ResultData editData(@RequestAttribute Long _userId, @RequestBody UsersManualCatalog usersManualCatalog) {

		usersManualCatalogService.editData(_userId, usersManualCatalog);
		return ResultData.success();
	}

	/**
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping("/delete")
	public ResultData delete(Long code) {
		if (code == null) {
			return ResultData.error(ResultData.PARAM_ERROR_CODE, "参数错误");
		}

		List<UsersManualFile> list = usersManualFileService.getListByXftId(code);

		if (list != null && list.size() > 0) {
			return ResultData.error(ResultData.PARAM_ERROR_CODE, "当前目录下已有文档，请先删除文档再删除目录");
		}

		usersManualCatalogMapper.deleteById(code);
		return ResultData.success();
	}

	/**
	 * 
	 * @param code
	 * @return
	 */
	@GetMapping("/getInfo")
	public ResultData getInfo(Long code) {
		if (code == null) {
			return ResultData.error(ResultData.PARAM_ERROR_CODE, "参数错误");
		}
		return ResultData.success("ok", usersManualCatalogService.getInfo(code));
	}

	/**
	 * @Title: getLeafNode
	 * @Description: 获取全部叶子节点
	 * @author dyl
	 * @date: 2021-01-28
	 * @return
	 * @return Result
	 */
	@GetMapping("/getAllLeafNode")
	public ResultData getAllLeafNode(Long xiangmuId) {

		// 获取个人创建数据
		List<UsersManualCatalog> list = new ArrayList<UsersManualCatalog>();

		UsersManualCatalog usersManualCatalog = new UsersManualCatalog();
		usersManualCatalog.setId(xiangmuId);
		usersManualCatalog.setParentId(-1L);
		usersManualCatalog.setSortNum(0);
		usersManualCatalog.setName("用户手册目录");
		usersManualCatalog.setXmId(xiangmuId);

		list.add(usersManualCatalog);

		List<UsersManualCatalog> list1 = usersManualCatalogService.getListByXmId(xiangmuId);
		if (list1 != null && list1.size() > 0) {
			list.addAll(list1);
		}

		return ResultData.success("ok", list);
	}

	/**
	 * 
	 * @param _userId
	 * @return
	 */
	@RequestMapping("/getInitialLoading")
	public ResultData getInitialLoading(@RequestAttribute Long _userId, Long xmId) {
		// 获取权限数据
		List<UsersManualCatalog> list = usersManualCatalogService.getListByUserIdAndType(_userId, xmId);
		return ResultData.success("ok", list);
	}

	/**
	 * 
	 * @param _userId
	 * @return
	 */
	@RequestMapping("/getAgainLoading")
	public ResultData getAgainLoading(@RequestAttribute Long _userId, Long xmId, Long parentId) {
		// 获取权限数据
		List<UsersManualCatalog> list = usersManualCatalogService.getListByUserIdAndTypeAndParentId(_userId, xmId,
				parentId);
		return ResultData.success("ok", list);
	}

}
